package rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.contant.Contants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Contants.HOST);
        connectionFactory.setPort(Contants.PORT);
        connectionFactory.setUsername(Contants.USER_NAME);
        connectionFactory.setPassword(Contants.PASSWORD);
        connectionFactory.setVirtualHost(Contants.VIRTUAL_HOST);
        Connection connection = connectionFactory.newConnection();

        // 2.开启信道
        Channel channel = connection.createChannel();
        // 3.声明交换机 使用内置交换机
        // 4.声明队列
        /**
         * Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments) throws IOException;
         * String queue: 队列名称
         * boolean durable: 是否可以持久化
         * boolean exclusive: 是否独占
         * boolean autoDelete: 是否自动删除
         * Map<String, Object> arguments: 参数
         */

        channel.queueDeclare("hello",true,false,false,null);
        // 5.发送消息
        /**
         * String exchange, String routingKey, BasicProperties props, byte[] body
         * exchange: 交换机名称
         * routingKey: 使用内置交换机,routingKey和队列名称保持一致
         * props: 属性
         * body: 发送的消息
         */
        for (int i=0;i<10;i++){
            String msg = "hello RabbitMQ: "+i;
            channel.basicPublish("","hello",null,msg.getBytes());
        }
        System.out.println("消息发送成功");
//        // 6.资源释放(有前后顺序)
//        channel.close(); // 可以省略
//        connection.close();

    }
}
